package com.wq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wq.common.CustomerException;
import com.wq.entity.Category;
import com.wq.entity.Dish;
import com.wq.entity.Setmeal;
import com.wq.mapper.CategoryMapper;
import com.wq.service.CategoryService;
import com.wq.service.DishService;
import com.wq.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ description
 * @ author wuyunpeng
 * @ create 2022-07-21 13:47
 */
@Service
public class CategoryServiceIml extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;

    /**
     * 只有当分类没有关联 菜品 和 套餐时才能删除
     *
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> lqwDish = new LambdaQueryWrapper<>();
        lqwDish.eq(Dish::getCategoryId, id);
        final int countDish = dishService.count(lqwDish);
        if (countDish > 0) {
            throw new CustomerException("当前分类关联了菜品，不能删除！");
        }
        LambdaQueryWrapper<Setmeal> lqwSetmeal = new LambdaQueryWrapper<>();
        lqwSetmeal.eq(Setmeal::getCategoryId, id);
        final int countSetmeal = setmealService.count(lqwSetmeal);
        if (countSetmeal > 0) {
            throw new CustomerException("当前分类关联了套餐，不能删除！");
        }
        //没有关联
        super.removeById(id);
    }
}
